function addRow(idTb,content1,content2,body,state,content5,content6,button)
{
         
         tabBody=document.getElementById(idTb);
         row=document.createElement("TR");
         cell1 = document.createElement("TD");
         cell2 = document.createElement("TD");
         cell3 = document.createElement("TD");
         cell4 = document.createElement("TD");
         cell5 = document.createElement("TD");
	 cell6 = document.createElement("TD");
	 cell7 = document.createElement("TD");
         
         textnode1=document.createTextNode(content1);
         textnode2=document.createTextNode(content2);
         //bodynode3=document.createElement("div");
         statenode=document.createElement("div");
	textnode5=document.createTextNode(content5);
	textnode6=document.createTextNode(content6);
         divNode7=document.createElement("div");
         
	
	//create body td
	body_lines=body.split("\n");
	
	for (i=1;i<body_lines.length;i++){
		body_row=document.createElement("TR");
		body_tag_cell=document.createElement("TD");
		body_value_cell=document.createElement("TD");
		//body_tag_text=document.createTextNode(body_lines[i].split(":")[0]);
		body_tag_text=document.createElement("div");
		body_tag_text.innerHTML="<b>"+body_lines[i].split(":")[0];
		var value=body_lines[i].split(":");
		
		if ((body_lines[i].split(":")).length>1) {
			var stringa=value[1]
			for(j=2;j<value.length;j++){
			stringa=stringa+":"+value[j];}
			body_value_text=document.createTextNode(stringa);}
		else{
			body_value_text=document.createTextNode(body_lines[i].split(":")[1]);}
		
		body_tag_cell.appendChild(body_tag_text);
		body_value_cell.appendChild(body_value_text);		
		body_row.appendChild(body_tag_cell);
		body_row.appendChild(body_value_cell);
		
		cell3.appendChild(body_row);
	}
         divNode7.innerHTML = button;
	 //bodynode3.innerHTML="<b>"+content3+"</b>";
         statenode.innerHTML="<b>"+state+"</b>";
         cell1.appendChild(textnode1);
         cell2.appendChild(textnode2);
         //cell3.appendChild(bodynode3);
         cell4.appendChild(statenode);
         cell5.appendChild(textnode5);
	 cell6.appendChild(textnode6);
	 cell7.appendChild(divNode7);
         
	cell3.width="500px";
         row.appendChild(cell1);
         row.appendChild(cell2);
         row.appendChild(cell3);
         row.appendChild(cell4);
         row.appendChild(cell5);
	 row.appendChild(cell6);
	 row.appendChild(cell7);
        
         tabBody.appendChild(row);
       
   
}

function cleanTableBody(idTb)
{
         //com
         tabBody=document.getElementById(idTb);
         
       while(tabBody.hasChildNodes())
		{
  			 tabBody.removeChild(tabBody.firstChild);
		}
         
       
   
}

function restore(uid,rev,entry_type,i){

answer=confirm("Restore revision "+rev+"?\n WARNING: this require Evolution Backend restart!");
if(answer){
var ajax = assegnaXMLHttpRequest(),usaLink = true;
if(ajax){
usaLink = false;

    ajax.open("get", "/versionsyncwebapp/app/ajax_restore/"+uid+"/"+rev+"/"+entry_type+"/", true);
    

    ajax.setRequestHeader("connection", "close");

ajax.onreadystatechange = function() {


      if(ajax.readyState === readyState.COMPLETATO) {

        if(statusText[ajax.status] === "OK"){
                var errors=ajax.responseText;
 if(errors!="noerrors"){
	alert("ERRORS while restoring!\n"+errors);

}
else{
alert("Restored successfully!");
        restore_button=document.getElementById("id_restore_button"+i);
        restore_button.disabled=true;
}
}
}
}
    ajax.send(null);

}
}
}


function load_data() {

 
  var ajax = assegnaXMLHttpRequest(),usaLink = true;
  
  var input=document.getElementById("id_result");
  var key=document.getElementById("AppRic").value;
  var entry_type=document.getElementById("id_entry_type").value;
  var entry_field=document.getElementById("id_entry_field").value
	//var debug_input=document.getElementById("id_result");
        //debug_input.value="http://127.0.0.1:8000/app/ajax_find/"+key+"/"+entry_type+"/"+entry_field+"/";
 
if(key==''){
cleanTableBody('tbResult');
document.getElementById("label_pages").innerHTML=0;
document.getElementById("label_result_size").innerHTML='';
document.getElementById("id_previous_button").disabled=true;
document.getElementById("id_next_button").disabled=true;
document.getElementById("id_next_button_down").disabled=true;
document.getElementById("id_previous_button_down").disabled=true;
document.getElementById("id_entries_per_page").disabled=true;
document.getElementById("current_page").innerHTML=0;

}
	
  if(ajax && entry_field!=-1 && entry_type!=-1 && key!='') {
  
    usaLink = false;
    
    ajax.open("get", "/versionsyncwebapp/app/ajax_find/"+key+"/"+entry_type+"/"+entry_field+"/", true);

    
    ajax.setRequestHeader("connection", "close");

    
    ajax.onreadystatechange = function() {
      
      
      if(ajax.readyState === readyState.COMPLETATO) {
       
        if(statusText[ajax.status] === "OK"){
		cleanTableBody('tbResult');
        	var objs= eval('('+ajax.responseText+')');
		document.getElementById("label_result_size").innerHTML="Found <b>"+objs.length+"</b> entries";
		if(objs.length>0){
		 document.getElementById("id_previous_button").disabled=false;
		 document.getElementById("id_next_button").disabled=false;
		 document.getElementById("id_next_button_down").disabled=false;
		 document.getElementById("id_previous_button_down").disabled=false;
		 document.getElementById("id_entries_per_page").disabled=false;
		}
        	for(var i=0; i < objs.length; i ++){
          		obj = objs[i];
			
			htmlRestoreButton='<input type="button" value="Restore" id="id_restore_button'+i+'" onclick="restore(\''+obj['uid']+'\',\''+obj['rev']+'\',\''+entry_type+'\',\''+i+'\');">';
          		//linkModifica = '<a href="/PyGuestbook/index.py/edit_dedica?id='+obj.id+'">Edit</a>&nbsp;&nbsp';
				//linkElimina = '<a href="/PyGuestbook/index.py/elimina_dedica?id='+obj.id+'">Elimina</a>';
          		addRow('tbResult',obj.uid,obj.rev,obj.body,obj.state,obj.timestamp,obj.removed_timestamp,htmlRestoreButton);
          		
          	}
	paginateTable('tbResult', 0);

        }else {
          
          //input. = "Impossibile effettuare l'operazione richiesta.<br />";
          //elemento.innerHTML += "Errore riscontrato: " + statusText[ajax.status];
        }
      } 
    }

   
    ajax.send(null);
  }
   
  return true;
}
